import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { FeatureScenarioService } from './feature-scenario.service';
import { CreateFeatureScenarioDto } from './dto/create-feature-scenario.dto';
import { UpdateFeatureScenarioDto } from './dto/update-feature-scenario.dto';
import { Api } from 'src/utils/api';
import { ApiBody, ApiOperation, ApiParam, ApiProperty, ApiTags } from '@nestjs/swagger';
import { CreateManyDto } from './dto/createMany.dto';
import { UpdateSortDto } from './dto/update-sort.dto';
import { SwaggerOk } from 'src/decorators/swagger.decorator';
 

 

@ApiTags('feature-scenario中间表接口')
@Controller('feature-scenario')
export class FeatureScenarioController {
  constructor(private readonly featureScenarioService: FeatureScenarioService) {}

  @Post()
  @SwaggerOk()
  async create(@Body() createFeatureScenarioDto: CreateFeatureScenarioDto) {
    await this.featureScenarioService.create(createFeatureScenarioDto);
    return Api.ok()
  }

  @Post('/createMany')
  async createMany(@Body() data: CreateManyDto){
    const {fid, sids} =data;
    if(fid){
      await this.featureScenarioService.deleteByFid(fid);
    }
    let res = sids.map(async(sid:number, idx:number)=>{
      await this.featureScenarioService.create({
        fid, sid, sort:idx+1
      })
    });

    await Promise.all(res);

    return  Api.ok()
  }


  
  @ApiOperation({summary:'更新排序'})
  @Post('/updateSort')
  @ApiBody({type:[UpdateSortDto]})
  async updateSort(@Body() data:UpdateSortDto[]){
    data.map( async(d)=>{
      const upRes = await this.featureScenarioService.updateSort(d.fid, d.sid, d.sort);
    })
    return  Api.ok()
  }

  @Get('/fid/:id')
  async findByFid(@Param('id') id: number) {
    const data=await  this.featureScenarioService.findManyByFid(+id);
    return Api.ok(data)
  }

 @Delete('/:fid/:sid')
 async remove(@Param('fid') fid: string, @Param('sid') sid: string) {
    await this.featureScenarioService.remove(+fid, +sid);
    return Api.ok();
  }

 

}
